/*
 * @Date: 2022-05-16 17:14:56
 * @LastEditors: sailornpg
 * @LastEditTime: 2022-05-18 09:42:31
 * @FilePath: \react-lvyou\src\store\shoppingCart\index.ts
 * @Description: 购物车redux
 */
import { createAsyncThunk, createSlice, PayloadAction } from "@reduxjs/toolkit";
import {
  reqShoppingCartList,
  reqAddShoppingCart,
  reqClearShoppingCart,
} from "@/api/user";
import { rootStateType } from "@/store/index";
import axios from "axios";
interface ShoppingCartType {
  loading: boolean;
  items: any[];
}
const initialState: ShoppingCartType = {
  loading: false,
  items: [],
};
export const fetchShoppingCartList = createAsyncThunk(
  "ShoppingCartSlice/fetchShoppingCartList",
  async (_, thunkAPI) => {
    const { User } = thunkAPI.getState() as any;
    let res = await axios.get(`http://123.56.149.216:8089/api/shoppingCart`, {
      headers: {
        Authorization: `bearer ${User.token!}`,
      },
    });
    console.log(res, "!@#!@#");
    return res.data.shoppingCartItems;
  },
);
export const fetchAddShoppingCart = createAsyncThunk(
  "ShoppingCartSlice/fetchAddShoppingCart",
  async (touristRouteId: string, thunkAPI) => {
    const { User } = thunkAPI.getState() as any;
    let { data } = await axios.post(
      `http://123.56.149.216:8089/api/shoppingCart/items`,
      {
        touristRouteId,
      },
      {
        headers: {
          Authorization: `bearer ${User.token!}`,
        },
      },
    );
    console.log(data, "!@#!@#");

    return data.shoppingCartItems;
  },
);
export const fetchClearShoppingCartList = createAsyncThunk(
  "ShoppingCartSlice/fetchClearShoppingCartList",
  async (itemsIds: number[], thunkAPI) => {
    const { User } = thunkAPI.getState() as any;
    return await reqClearShoppingCart(User.token!, itemsIds);
  },
);
export const checkout = createAsyncThunk(
  "ShoppingCartSlice/checkout",
  async (_, thunkAPI) => {
    const { User } = thunkAPI.getState() as any;
    const { data } = await axios.post(
      `http://123.56.149.216:8089/api/shoppingCart/checkout`,
      null,
      {
        headers: {
          Authorization: `bearer ${User.token!}`,
        },
      },
    );
    return data;
  },
);
export const ShoppingCartSlice = createSlice({
  name: "ShoppingCartSlice",
  initialState,
  reducers: {},
  extraReducers: {
    [fetchShoppingCartList.pending.type]: (state) => {
      state.loading = true;
    },
    [fetchShoppingCartList.fulfilled.type]: (state, action) => {
      state.loading = false;
      state.items = action.payload;
    },
    [fetchShoppingCartList.rejected.type]: (state) => {
      state.loading = false;
    },
    [fetchAddShoppingCart.pending.type]: (state) => {
      state.loading = true;
    },
    [fetchAddShoppingCart.fulfilled.type]: (state, action) => {
      state.loading = false;
      state.items = action.payload;
    },
    [fetchAddShoppingCart.rejected.type]: (state) => {
      state.loading = false;
    },
    [fetchClearShoppingCartList.pending.type]: (state) => {
      state.loading = true;
    },
    [fetchClearShoppingCartList.fulfilled.type]: (state) => {
      state.loading = false;
      state.items = [];
    },
    [fetchClearShoppingCartList.rejected.type]: (state) => {
      state.loading = false;
    },
    [checkout.pending.type]: (state) => {
      state.loading = true;
    },
    [checkout.fulfilled.type]: (state, action) => {
      state.items = [];
      state.loading = false;
    },
    [checkout.rejected.type]: (state, action: PayloadAction<string | null>) => {
      state.loading = false;
    },
  },
});
